package com.revai.engine.basic.controller;

import com.revai.engine.common.api.CommonResult;

import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.github.pagehelper.PageInfo;
import com.revai.engine.basic.service.TableService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.net.URLEncoder;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

/**
 * Created by CodeGenerator on 2024/01/11.
 */
@Api(tags = "TableController", description = "表格查询通用接口")
@Controller
@RequestMapping("${server.api-path}/table")
public class TableController {

    private final Logger logger = LoggerFactory.getLogger(TableController.class);

    @Resource
    private TableService tableService;

    @ApiOperation(value = "查询表数据")
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Map<String, Object>> search(@RequestBody JSONObject requestBody) {
        try {
            List<?> list = tableService.getTableData(requestBody);

            PageInfo<List<?>> pageInfo = new PageInfo(list);
            Map<String, Object> page = new HashMap<>();
            page.put("DataCount", pageInfo.getTotal());
            page.put("NowPage", pageInfo.getPageNum());
            page.put("OnePageCount", pageInfo.getSize());
            page.put("PageCount", pageInfo.getPages());
            page.put("StartIndex", pageInfo.getStartRow());
            page.put("OrderBy", null);

            Map<String, Object> result = new HashMap<>();
            result.put("Page", page);
            result.put("List", list);
            return CommonResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "导出表数据")
    @RequestMapping(value = "/searchExport", method = RequestMethod.POST)
    @ResponseBody
    public void searchExport(@RequestBody JSONObject requestBody, HttpServletResponse response) {
        try {
            String fileName = "导出文件.xlsx"; // 通过requestBody来取
            response.addHeader("Content-Disposition", "attachment;filename*=UTF-8''" + URLEncoder.encode(fileName, "UTF-8"));//这种方式mac和windows导出的文件名就不会乱码
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/octet-stream");
            response.addHeader("Content-Transfer-Encoding", "binary");
        
            // 导出excel到输出流
            tableService.exportExcelToOutputStream(requestBody, response.getOutputStream());
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "根据id查询一条记录")
    @RequestMapping(value = "/item", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> item(@RequestBody JSONObject requestBody) {
        try {
            return CommonResult.success(tableService.getTableItem(requestBody));
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "新增表数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> add(@RequestBody JSONObject requestBody) {
        try {
            return CommonResult.success(tableService.addTableData(requestBody));
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "更新表数据")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> edit(@RequestBody JSONObject requestBody) {
        try {
            return CommonResult.success(tableService.updateTableData(requestBody));
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "删除表数据")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> delete(@RequestBody JSONObject requestBody) {
        try {
            tableService.deleteTableData(requestBody);
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }
}
